/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JplManagement;

import aimax.osm.data.Position;
import aimax.osm.data.entities.MapNode;
import aimax.osm.data.impl.DefaultMap;
import java.util.*;
import com.google.gson.*;
import java.io.File;
import JplManagement.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import parserosm.ParserOsm;

/**
 *
 * @author Corlo
 */
public class Salida {

    public static ArrayList<nodosRecorridos> generarSalida(List<Position> nodosRecorridos, ParserOsm parseador) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Resultado output = new Resultado();
        DefaultMap map = parseador.gethand();
        //String resultado="[";
        Field f = null; //NoSuchFieldException
        nodosRecorridos nodosre=new nodosRecorridos();
        ArrayList<nodosRecorridos> lista= new ArrayList<nodosRecorridos>();
        Hashtable<Long, MapNode> nodos = null; //IllegalAccessException


        ListIterator<Position> iterator = nodosRecorridos.listIterator();
        f = map.getClass().getDeclaredField("nodes"); //NoSuchFieldException
        f.setAccessible(true);
        nodos = (Hashtable) f.get(map); //IllegalAccessException

        while (iterator.hasNext()) {
            Enumeration<Long> keys = nodos.keys();
            long nextelement = keys.nextElement();
            //long id = 0;
            Position getrecorrido = iterator.next();
            for (int i = 0; i < nodos.size(); i++) {
                MapNode get = nodos.get(nextelement);
                //id = get.getId();
                if (get.getLat() == getrecorrido.getLat() && get.getLon() == getrecorrido.getLon()) {
                    nodosre.nodeId=get.getId();
                    lista.add(nodosre);
                    //id = get.getId();
                    break;
                }
                nextelement = keys.nextElement();
            }
        }

        return lista;
    }

    public static void escribirjson(String json, String fileName, boolean append){
        try {
          // A partir del objeto File creamos el fichero físicamente
            //String fileName = "files\\salida.json";
            FileWriter fw;
            fw = new FileWriter(fileName, append);

            PrintWriter pw = new PrintWriter(fw);
            pw.println(json);
            pw.close();
            fw.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
